const { login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { set } = require('./../db/redis')

const handleUserRouter = (req, res) => {
   const method = req.method;
   //登录
   if (method === 'POST' && req.path === '/api/user/login') {
      // const { username, password } = req.query
      const { username, password } = req.body
      const result = login(username, password)
      return result.then(data => {
         if (data.username) {
            req.session.username = data.username
            req.session.realname = data.realname
            // update redis
            console.log('sessionID: ',req.sessionId,req.session)
            set(req.sessionId, req.session)
            return new SuccessModel()
         }
         return new ErrorModel('登录失败')
      })
   }
   //登录验证
   // if (method === 'GET' && req.path === '/api/user/login-test') {
   //    if (req.session.username) {
   //       return Promise.resolve(new SuccessModel(
   //          {
   //             session: req.session,
   //             msg: '用户已经登录'
   //          }
   //       ))
   //    } else {
   //       return Promise.reject(new ErrorModel('未登录'))
   //    }
   // }
}
module.exports = handleUserRouter